﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using EluZSProvider;
using System.Web;
using System.Data;
using Antu.Core.DataAccess;

namespace WebSys.UI.Control.ZhengShou
{
    public class ZS_PGJGInfoControl : MPBasicTemplate
    {
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        Label lbl项目名称, lbl征收范围;
        TextBox txt征收评估机构, txt_ybm, txt已通过征收评估机构, txt抽签地点;
        DropDownList ddl选择评估机构, DropDownList1;
        Button btn_xz, Button2, btn_sq, btn_yzg, btn_printGG;
        MPCalendar txt抽签时间;
        string strpid = string.Empty;
        public string userid = string.Empty;
        protected override void OnLoad(EventArgs e)
        {
            if (HttpContext.Current.Session["UserID"] == null)
            {
                Antu.Utilities.UtilitiesPage.AlertDialogClear("登录超时，请重新登录！", "window.parent.location.href('../login.aspx');");
            }
            else
            {
                userid = HttpContext.Current.Session["UserID"].ToString();
            }
            base.OnLoad(e);
            InitControl();
            Button btnUpload = (Button)FindControl("btnUpload");
            Button BtnSave = (Button)FindControl("BtnSave");
            btn_sq = (Button)FindControl("btn_sq");
            btn_sq.Click += new EventHandler(btn_sq_Click);
            Button btnRNull = (Button)FindControl("btnRNull");
            btn_xz = (Button)FindControl("btn_xz");
            btn_xz.Click += new EventHandler(btn_xz_Click);
          //  btnRNull.Click+=new EventHandler(btnRNull_Click);
            BtnSave.Click += new EventHandler(BtnSave_Click);
            btnUpload.Click += new EventHandler(btnUpload_Click);
            Button2 = (Button)FindControl("Button2");
            Button2.Click += new EventHandler(Button2_Click);
            strpid = base.Page.Request["PID"];
            string strpnid = base.Page.Request["PNID"];
            DropDownList1 = (DropDownList)FindControl("DropDownList1");
            txt征收评估机构 = (TextBox)FindControl("txt征收评估机构");
            txt抽签地点 = (TextBox)FindControl("txt抽签地点");
            txt抽签时间 = (MPCalendar)FindControl("txt抽签时间");
            if (!base.Page.IsPostBack)
            {
                if (!string.IsNullOrEmpty(strpid))
                {
                    EluZSProvider.Project project = _EluZSDataProvider.GetProjectXX(strpid);
                    lbl项目名称.Text = project.项目名称;
                    lbl征收范围.Text = project.征收范围;
                    //txt_ybm.Text = project.候选评估机构;
                }
                if (!string.IsNullOrEmpty(strpnid)) {
                    EluZSProvider.Prono prono = GetProno(strpnid);
                    ddl选择评估机构.SelectedValue = prono.选择评估机构;
                    txt征收评估机构.Text = prono.征收评估机构;
                    txt已通过征收评估机构.Text = prono.已通过征收评估机构;
                    txt抽签地点.Text = prono.抽签地点;
                    if (prono.抽签时间.ToString("yyyy-mm-dd") != "0001-00-01")
                    {
                        txt抽签时间.Text = prono.抽签时间.ToString();
                    }
                    else
                    {
                        txt抽签时间.Text = "";
                    }
                }
            }
            string sql = @"Select 单位名称
  From Zs_Company z,Zs_Pgbmjl p
 Where z.Id = p.comid
 And p.pnid = '" + Page.Request["pnid"] + @"' Order By 报名时间 Asc";
            DataTable dt = _EluZSDataProvider.GetTable(sql);
            if (dt.Rows.Count>0)
            {
                txt_ybm.Text = "";
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    txt_ybm.Text += dt.Rows[i]["单位名称"].ToString()+",";
                }
                txt_ybm.Text = txt_ybm.Text.Substring(0, txt_ybm.Text.Length - 1);
            }

            string sql3 = @"Select 单位名称
  From Zs_Company z,Zs_Pgbmjl p
 Where z.Id = p.comid
 And p.pnid = '" + Page.Request["pnid"] + @"'  And 审批状态 = '1'
               Order By 报名时间 Asc";
            DataTable dt1 = _EluZSDataProvider.GetTable(sql3);
            if (dt1.Rows.Count > 0)
            {
                txt征收评估机构.Text = "";
                for (int i = 0; i < dt1.Rows.Count; i++)
                {
                    txt征收评估机构.Text += dt1.Rows[i]["单位名称"].ToString() + ",";
                }
                txt征收评估机构.Text = txt征收评估机构.Text.Substring(0, txt征收评估机构.Text.Length - 1);
            }
            IsLookPerson();
            IsQuPerson();

            string sql2 = "Select rolename from zs_role where id in (Select roleid from zs_user_in_role where userid = '" + userid + "') and rolename like '%区征收%' ";
            DataTable dt2 = _EluZSDataProvider.GetTable(sql2);
            if (dt2.Rows.Count > 0)
            {
                string sql1 = "Select * from zs_project where id= '" + strpid + "'";
                DataTable dt3 = _EluZSDataProvider.GetTable(sql1);

                if (dt3.Rows.Count>0)
                {
                    if (dt3.Rows[0]["是否抽签"].ToString() =="1")
                    {
                        btn_xz.Enabled = false;
                        //BtnSave.Enabled = false;
                    }
                }
            }

            string jsstrIP = base.Page.Request.ServerVariables["HTTP_HOST"];
            //判断是否是内网用户，如果是代表是征收办人员，可以选择已确定评估机构
            if (jsstrIP == System.Configuration.ConfigurationManager.AppSettings["FCSERVER"])
            {
                btn_xz.Enabled = true ;
            }
   
        }

        protected void btn_sq_Click(object sender, EventArgs e)
        {
            string strpid = base.Page.Request["PID"];
            string sql = "update zs_project set 部门申请状态 = '1' where id = '" + strpid + "'";
            _EluZSDataProvider.RunSqlNon(sql);
            Page.ClientScript.RegisterStartupScript(this.GetType(), "al", "<script>alert('申请成功！');</script>");
            btn_sq.Enabled = false;
        }

        public Prono GetProno(string pnid)
        {
            Prono project = new Prono();
            string sql = @"select * from zs_prono where pid ='" + pnid + "'";
            DataTable dt = null;
            dt = _EluZSDataProvider.GetTable(sql);
            return project = DataHelper.DataTableToTOne<Prono>(dt);
        }

        protected void btn_xz_Click(object sender, EventArgs e)
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "href", "<script>select_project2('"+Page.Request["Pnid"]+"');</script>");
        }

        private void IsLookPerson()
        {
            string sql = "Select rolename from zs_role where id in (Select roleid from zs_user_in_role where userid = '" + userid + "')";
            DataTable dt = _EluZSDataProvider.GetTable(sql);
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (dt.Rows[i]["rolename"].ToString().Equals("查看人员"))
                    {
                        Button btnSave = (Button)FindControl("btnSave");
                        btnSave.Visible = false;
                    }
                }
            }
        }

        private void IsQuPerson()
        {
            if (!_EluZSDataProvider.IsQZSBM(userid) && !_EluZSDataProvider.IsZSAdmin(userid) && _EluZSDataProvider.IsNetZSAdmin(userid))
            {
                btn_sq.Visible = false;
            }
            else
            {
                string sql = "Select * from zs_project where id= '" + strpid + "'";
                DataTable dt = _EluZSDataProvider.GetTable(sql);
                if (dt.Rows.Count>0)
                {
                    if (dt.Rows[0]["部门申请状态"].ToString() == "1")
                    {
                        btn_sq.Enabled = false;
                    }
                }
            }

        }


        protected void InitControl()
        {
            lbl项目名称 = (Label)FindControl("lbl项目名称");
            lbl征收范围 = (Label)FindControl("lbl征收范围");
            txt征收评估机构 = (TextBox)FindControl("txt征收评估机构");
            txt已通过征收评估机构 = (TextBox)FindControl("txt已通过征收评估机构");
            ddl选择评估机构 = (DropDownList)FindControl("ddl选择评估机构");
            txt_ybm = (TextBox)FindControl("txt_ybm");
            btn_printGG = (Button)FindControl("btn_printGG");
            btn_printGG.Click += new EventHandler(btn_printGG_Click);
        }


        /// <summary>
        /// 打印公告
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_printGG_Click(object sender, EventArgs e)
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "print", "<script>location.href=('../ZSManage/zs_projectDYEdit.aspx?ID="+Page.Request["Pnid"]+"&type=zggg');</script>");
        }

        protected void BtnSave_Click(object sender, EventArgs e)
        {
            string strpid = base.Page.Request["PID"];
            string strpnid = base.Page.Request["PNID"];
            EluZSProvider.Prono prono = GetPronos(strpnid);
            string sql2 = "Select * from zs_project where id = '" + Page.Request["pnid"] + "'";
            DataTable dt1 = _EluZSDataProvider.GetTable(sql2);
            if (dt1.Rows.Count>0)
            {
                if (dt1.Rows[0]["是否抽签"].ToString() == "1")
                {
                    prono.选择评估机构 = ddl选择评估机构.SelectedValue;
                }
                else
                {
                    prono.选择评估机构 = DropDownList1.SelectedValue;
                }
            }
            prono.PID = strpnid;
            prono.征收评估机构=txt征收评估机构.Text ;
            prono.已通过征收评估机构 = txt已通过征收评估机构.Text;
            prono.抽签地点 = txt抽签地点.Text;
            if (!string.IsNullOrEmpty(txt抽签时间.Text.ToString()))
            {
                prono.抽签时间 = Convert.ToDateTime(txt抽签时间.Text);
            }
            

            string sql = @"Select * from ZS_PGBMJL where pnid ='" + strpnid + "' and comid in (Select id from zs_company where 单位名称 = '" + txt已通过征收评估机构.Text + "')";
            DataTable dt = _EluZSDataProvider.GetTable(sql);
            if (dt.Rows.Count>0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string sql1 = "update ZS_PGBMJL set 通过状态 = 1 where id = '" + dt.Rows[i]["id"] + "'";
                    _EluZSDataProvider.RunSqlNon(sql1);
                }
            }

            string uppro = "Select * from zs_prono where pid = '" + strpid + "'";
            DataTable updt = _EluZSDataProvider.GetTable(uppro);
            bool save;
            if (updt.Rows.Count>0)
            {
                 save = SaveZSPGJG(strpid, prono);
            }
            else
            {
                 save = SaveZSPGJG("", prono);
            }


            string sqlpro = "update zs_project set 部门申请状态 = '1' where id = '" + strpid + "'";
            _EluZSDataProvider.RunSqlNon(sqlpro);

            if (_EluZSDataProvider.CkFileCount(strpid, "8")) { _EluZSDataProvider.UpdateFileCS(strpid, "8"); }
            if (save) { base.Page.Response.Write("<script>alert('保存成功！');window.location.href('ZS_PGJGInfo.aspx?PID=" + strpid + "&PNID="+strpnid+"');</script>"); }
        }


        public Prono GetPronos(string pnid)
        {
            Prono project = new Prono();
            string sql = @"select * from zs_prono where pid ='" + pnid + "'";
            DataTable dt = _EluZSDataProvider.GetTable(sql);
            
            return project = DataHelper.DataTableToTOne<Prono>(dt);
        }


        public bool SaveZSPGJG(string strid, Prono prono)
        {
            string sql = string.Empty;
            if (!string.IsNullOrEmpty(strid))
            {
                sql = DataHelper.UpdateT<Prono>(prono, "ZS_PRONO", new string[] { "pid" });
            }
            else
            {
                sql = DataHelper.AddT<Prono>(prono, "ZS_PRONO");
            }
            DataTable dt = _EluZSDataProvider.GetTable(sql);
            if (dt.Rows.Count>0)
            {
                return false;
            }
            else
            {
                return true;
            }
            
        }


        protected void btnRNull_Click(object sender, EventArgs e)
        {
            string strpnid = base.Page.Request["PNID"];
            _EluZSDataProvider.UpdatePGJGNull("zs_prono", "征收评估机构", strpnid);
            base.Page.Response.Write("<script>alert('清空成功！');window.location.href=window.location.href;</script>");
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            string strpnid = base.Page.Request["PNID"];
            _EluZSDataProvider.UpdatePGJGNull("zs_prono", "已通过征收评估机构", strpnid);
            base.Page.Response.Write("<script>alert('清空成功！');window.location.href=window.location.href;</script>");
        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            //设置文件上传的关键字段
            if (!string.IsNullOrEmpty(base.Page.Request["PNID"]))
            {
                string strScript = string.Format("<script>OpenFileUpload('{0}','{1}','{2}');</script>",
                    base.Page.Request["PNID"].Trim(), base.Page.Request["PNID"].Trim(), base.Page.Request["PID"].Trim());
                Page.ClientScript.RegisterStartupScript(this.GetType(), "opennew", strScript);
            }
        }
    }
}


